/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.dao.ClassesDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Clas;
import vn.webapp.model.Classes;
import vn.webapp.model.Classing;
import vn.webapp.model.Coursing;
import vn.webapp.model.Student;
import vn.webapp.model.User;
import vn.webapp.model.Users;

@Service("classesService")
public class ClassesServiceImpl implements ClassesService{

    @Autowired
    private ClassesDAO classesDAO;

    public void setClassesDAO(ClassesDAO classesDAO) {
        this.classesDAO = classesDAO;
    }

    /**
     * 
     * @return 
     */
    @Override
    @Transactional
    public DataPage<Classes> list() {
        DataPage<Classes> dataClasses = new DataPage<>();
        
        dataClasses.setData(classesDAO.list());
        return dataClasses;
    }
    
    @Override
    @Transactional
    public Set<Clas> listTesting() {
        
        // Only classes
        DataPage<Classes> dataClasses = new DataPage<>();
        dataClasses.setData(classesDAO.list());
        List<Classes> claes = dataClasses.getData();
        
        // Combination classes and students
        DataPage<Clas> dataClases = new DataPage<>();
        dataClases.setData(classesDAO.listTesting());
        List<Clas> cla = dataClases.getData();
        
        Set<Clas> formatedData = new HashSet<Clas>(cla);
        
        return formatedData;
    }
    
    @Override
    @Transactional
    public DataPage<Classes> filter(int page, int pageSize) {
        DataPage<Classes> dataClasses = new DataPage<>();
        dataClasses.setPageIndex(page);
        dataClasses.setPageSize(pageSize); 
        dataClasses.setDataCount(classesDAO.count());
        dataClasses.setPageCount(dataClasses.getDataCount()/pageSize);
        if(dataClasses.getDataCount()%pageSize > 0){
            dataClasses.setPageCount(dataClasses.getPageCount()+1);
        }
        dataClasses.setData(classesDAO.filter(page, pageSize));
        return dataClasses;
    }
    
    @Override
    @Transactional
    public DataPage<Clas> filterTesting(int page, int pageSize){
        DataPage<Clas> dataClasses = new DataPage<>();
        dataClasses.setPageIndex(page);
        dataClasses.setPageSize(pageSize); 
        dataClasses.setDataCount(classesDAO.count());
        dataClasses.setPageCount(dataClasses.getDataCount()/pageSize);
        if(dataClasses.getDataCount()%pageSize > 0){
            dataClasses.setPageCount(dataClasses.getPageCount()+1);
        }
        dataClasses.setData(classesDAO.filterTesting(page, pageSize));
        
        
        return dataClasses;
    }
    
    @Override
    @Transactional
    public Classes viewDetail(String id) {
        return classesDAO.viewDetail(id);
    }
    
    @Override
    @Transactional
    public Classes checkExistsCode(String code, String id)
    {
        return classesDAO.checkExistsCode(code, id);
    }
    
    @Override
    @Transactional
    public int removeClass(String id) {
        return classesDAO.removeClass(id);
    }
    
    @Override
    @Transactional
    public int updateTheClass(String code, String id, String name)
    {
        return classesDAO.updateTheClass(code, id, name);
    }
    
    @Override
    @Transactional
    public int saveAClass(String code, String name)
    {
        Classes classes = new Classes();
        classes.setCode(code);
        classes.setName(name);
        return classesDAO.saveAClass(classes);
    }
}
